
package DAO;

import Utils.HibernateUtil;
import java.util.List;
import model.Category;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import validator.e;

public class CategoryDA extends BaseDAO<Category>{
    
    public void save(Category category){
        Session session = HibernateUtil.getSessionFactory().openSession(); 
     Transaction t = session.beginTransaction();        
     
     if(consult(category.getName())){
        try{
           session.save(category);
           t.commit();
           e.exibirMsgSucesso("Sucess");
        }
        catch(HibernateException err){
            e.exibirMsgErro("Error - Try Again");
        }
    }else
    {
        e.exibirMsgErro("Error - Category name already exists, Try Again!");
    }
     session.close();
    }

    @Override
    public List<Category> list() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query= session.createSQLQuery("select * from Category").addEntity(Category.class);
        List list = query.list();
        session.close();
        return list;
    }

    @Override
    public void update(Category category) {
       Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Category c = new Category();
        c.setCategory_id((category.getCategory_id()));
        c.setGoal(category.getGoal());
        c.setName(category.getName());
        try{
        session.update(c);
        t.commit();
        e.exibirMsgSucesso("Sucess");
        }
        catch(HibernateException err){
            e.exibirMsgErro("Error");
        }
        session.close();
    }

    @Override
    public void remove(Category category) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        if(consultRemove(category.getCategory_id())){
        try{
        session.delete(category);
        t.commit();
        e.exibirMsgSucesso("Sucess");
        }catch(HibernateException err){
            e.exibirMsgErro("Error - Try Again!");
        }
        }else{
            e.exibirMsgErro("Error - Category contains metrics associated");
        }
        session.close();
    }

    @Override
    public boolean consult(String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query= session.createSQLQuery("select * from Category where name = :name").addEntity(Category.class);
        query.setParameter("name",name);
        List lista = query.list();
        if(lista == null || lista.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
    
    public boolean consultRemove(int id){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query= session.createSQLQuery("select * from Metric where category_id = :id").addEntity(Category.class);
        query.setParameter("id",id);
        List lista = query.list();
        if(lista == null || lista.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
    
    
}
